/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJOS.Libro;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Izzabel
 */
public class DAO {
    public static List<Libro> getCustomer()
      {
          
             Session session = null;
             Configuration configuration=new Configuration();
             SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
             session=sessionFactory.openSession();
             Transaction transaction=session.beginTransaction();
         try
         {
            configuration.configure();
            session.beginTransaction();
            String HQL_QUERY= "from Libro libros";
            org.hibernate.Query query= session.createQuery(HQL_QUERY);
            
            System.out.println("Reading values...");
            
            List registro=query.list();
            List<Libro> act=new ArrayList<Libro>();
            
            boolean found=false;
            for(Iterator it= query.iterate(); it.hasNext();)
            {
                
                Libro a= (Libro) it.next();
                
                
                act.add(a);
                found=true;
            }
            session.getTransaction().commit();
            System.out.println("Done!");
            
           if(found)
           {
             return act;
           }
           
            
         }catch(Exception e)
         {
           System.out.println(e.getMessage());
         }
         finally
         {
           session.flush();
           session.close();
           }        
          return null;
         }
       public void altas(Libro lb)
       {
           Session session = null;
        try{
                Configuration configuration=new Configuration();
                SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
                session=sessionFactory.openSession();
                Transaction transaction=session.beginTransaction();

                System.out.println("Añadiendo un nuevo actor");
               // Actor actor=new Actor();
               // actor.setFirstName("Luis Alberto");
                //actor.setLastName("Ruiz Aguilar");

                session.save(lb);
                session.getTransaction().commit();

                System.out.println("Done!");
        }catch(Exception e){
            System.out.println(e.getMessage());
        } 
        finally{
            session.flush();
            session.close();
        }
       }
       public void bajas(Libro lb){
           Session session = null;
        try{
            Configuration configuration=new Configuration();
            SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
            session=sessionFactory.openSession();
            Transaction transaction=session.beginTransaction();
            
            session.delete(lb);
            transaction.commit();
            System.out.println("Hecho!");
        }catch(Exception e){ 
            System.out.println(e.getMessage());
        }
        finally{
            session.flush();
            session.close();
        }
       }
       public List consultar(){
           Session session = null;
       
            Configuration configuration=new Configuration();
            SessionFactory sessionFactory=configuration.configure().buildSessionFactory();
            session=sessionFactory.openSession();
            Transaction transaction=session.beginTransaction();
            //session.equals(this);
           Query query=session.createQuery("From Libro");
            List<Libro> lista= query.list();
     return lista;
       }
       
       
    
}
